import mysql = require('mysql');
 
const pool = mysql.createPool({
  // host     :  'rm-bp1p65v3w963naae9go.mysql.rds.aliyuncs.com',
  // user     :  'antown',
  // password :  'St02596001',
  // database :  'eyou_watch'
  // host     :  '47.111.244.94',
  // user     :  'root',
  // password :  'st02596001',
  // database :  'eyou_watch'
  host     :  'localhost',
  user     :  'root',
  password :  'root',
  database :  'vue-next-admin-new'
})

export default class Dbpool{
  //查询多条、新增、编辑、删除
  query(sql, params={}){
    // 返回一个 Promise
    return new Promise(( resolve, reject ) => {
      pool.getConnection(function(err, connection) {
        if (err) {
          reject( err )
        } else {
          connection.query(sql, params, ( err, rows) => {
            if ( err ) {
              reject( err )
            } else {
              resolve( rows )
            }
            // 结束会话
            //connection.release()
            pool.releaseConnection(connection);
          })
        }
      })
    })
  }

  //查询第一条
  queryOne(sql, params={}){
    // 返回一个 Promise
    return new Promise(( resolve, reject ) => {
      pool.getConnection(function(err, connection) {
        if (err) {
          reject( err )
        } else {
          connection.query(sql, params, ( err, rows) => {
            if ( err ) {
              reject( err )
            } else {
              resolve( rows[0] )
            }
            // 结束会话
            // connection.release()
            pool.releaseConnection(connection);
          })
        }
      })
    })
  }
}